﻿import com.gamepackage.events.*;
import com.gamepackage.util.*;

class com.gamepackage.util.LoadQueue
{
	private var queue: Array;
	
	public var addListener: Function;
	public var removeListener: Function;
	private var broadcastMessage: Function;
	
	function LoadQueue()
	{
		queue = new Array;
		
		AsBroadcaster.initialize( this );
	}
	
	function push( loadable: Loadable, url: String, info: Object ): Void
	{
		var lc: LoadClip = new LoadClip( loadable, url, info );
		
		lc.addListener( this );
		
		queue.push( lc );
	}
	
	//-- FORWARDING LOADCLIP EVENTS TO LISTENERS --//
	//
	function onLoadClipRequest(): Void
	{
		var args: Array = [ 'onLoadClipRequest' ].concat( arguments );
		broadcastMessage.apply( this , args );
	}
	
	function onLoadClipStart(): Void
	{
		var args: Array = [ 'onLoadClipStart' ].concat( arguments );
		broadcastMessage.apply( this , args );
	}
	
	function onLoadClipProcess(): Void
	{
		var args: Array = [ 'onLoadClipProcess' ].concat( arguments );
		broadcastMessage.apply( this , args );
	}
	
	function onLoadClipComplete(): Void
	{
		var args: Array = [ 'onLoadClipComplete' ].concat( arguments );
		broadcastMessage.apply( this , args );
	}
	
	function onLoadClipInit(): Void
	{
		var args: Array = [ 'onLoadClipInit' ].concat( arguments );
		broadcastMessage.apply( this , args );
		
		getNext();
	}
	
	function onLoadClipFailed(): Void
	{
		var args: Array = [ 'onLoadClipFailed' ].concat( arguments );
		broadcastMessage.apply( this , args );
	}
	
	//
	// ------------------------------------------- //
	//
	
	function load(): Void
	{
		broadcastMessage( 'onLoadQueueStart' );
		getNext();
	}
	
	function getNext(): Void
	{
		var lc: LoadClip = LoadClip( queue.shift() );
		if ( lc )
		{
			lc.load();
		}
		else
		{
			broadcastMessage( 'onLoadQueueComplete' );
		}
	}
}